$(function() {
	$('#qst').change(function(){
		if('' != $('#qen').val()){
			if(Date.parse($('#qst').val()) > Date.parse($('#qen').val())){
				$('#qen').val('');
			}
		}
		$('#qen').attr('min', $('#qst').val());
	});

});

//保存数据事件
function save(){
	var questionData = {};
	questionData.QUESTIONNAIRETITLE = $("#questionnaireTitle").val();
	questionData.QUESTIONNAIREDISC = $("#disc").val();
	questionData.EFFECTIVESTARTDATE = $("#qst").val();
	questionData.EFFECTIVEENDDATE = $("#qen").val();
	questionData.STATUS = $("input[name='STATUS']:checked").val();
	if (!check('main')){
		return true;
	}
	// 问题数组
	var questionDetailDatas = $('#questionDetail').serializeObjectArray();
	if(questionDetailDatas.length == 0){
		window.wxc.xcConfirm('请添加问卷问题', window.wxc.xcConfirm.typeEnum.error);
		return;
	}
	$.each(questionDetailDatas, function(index, obj){
		obj.QUESTIONORDER = index + 1;
	});
	
	questionData.questions = questionDetailDatas;
	$.ajax({
		method : 'post',
		url : basePath() + 'question/insert.do',
		data : JSON.stringify(questionData),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
			window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success, {
	    		onOk: function(){
	    			if(data.result == 1){
	    				parent.closeModal('modalMenu');		//刷新数据
	    			} 
	    		}
	    	});
		}
	});

}

function check(filedContent) {
	var result = true;
	var mustStr = "";
	if('main' == filedContent) {
		mustStr = "QUESTIONNAIRETITLE,QUESTIONNAIREDISC,EFFECTIVESTARTDATE,EFFECTIVEENDDATE";
	} else {
		mustStr = "QUESTION,QUESTIONTYPE,QUESTIONITEM";
	}
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for (var i = 0; mustStr != '' && i < mustStrs.length; i++) {
		var obj = $('[name="' + mustStrs[i] + '"]');
		var obj1 = obj.attr("placeholder");
		if (obj.val() == "") {
			strMsg += obj1.substring(0, 3)
					+ "<span style='color:red;'><strong>"
					+ obj1.substring(3, obj1.lengh) + "</strong></span>！<br />";
			result = false;
		}
	}
	if (strMsg != "") {
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	return result;
}
